/**
 *  Module
 *
 * Description
 */
angular.
module('sfhtHaitaoCmsApp')
    .controller('ModalAddDataCtrl', function($scope, $route, $injector, $modal, $modalInstance, item, nations, tabId, listId, Uploader) {
        var commonBizService = $injector.get("CommonBiz");
        $scope.items = item; //data属性字段
        $scope.nations = nations;

        _.each(item, function(item) {
            if (item.fieldName == "tag") {
                item.propertyValue = tabId;
            }
        });

        function checkProperties(items){
            var item = null;
            var temp = null;
            for(var i = 0, len = items.length; i < len; i++){
                item = items[i];
                if(item.fieldName == "link"){
                    if(!window.checkData.checkLink(item.propertyValue)){
                       temp = item.fieldDescription + "非法！！";
                       break;
                    }
                }
                if(item.fieldName == "h5link"){
                    if(!window.checkData.checkH5Link(item.propertyValue)){
                       temp = item.fieldDescription + "非法！！";
                       break;
                    }
                }
            }
            return temp;
        }

        //保存操作
        $scope.save = function(items) {
            var dataList = [];
            var addList = [];
            var properties = [];
            //校验item，link和h5link
            var errorInfo = checkProperties(items);
            if(errorInfo != null){
                alert(errorInfo);
                return false;
            }
            _.each(items, function(item) {
                properties.push({
                    propertyName: item.fieldName,
                    propertyValue: item.propertyValue
                });
            });

            var dataList = [{
                listId: listId,
                properties: properties
            }];
            commonBizService.addCmsData(dataList)
                .then(function(data) {
                    // window.location.reload(true);
                    $route.reload();
                }, function(data) {
                    alert("新增失败！！" + data);
                    console.log("error:" + data);
                })

            // 关闭页面
            $modalInstance.close(addList);
        };

        $scope.cancel = function() {
            $modalInstance.dismiss('cancel');
        };

        $scope.changeFile = function(slide) {
            Uploader.uploadFile($scope, slide, "image");
        };

        $scope.isProduct = function(items) {
            var item = _.findWhere(items, {
                'fieldName': 'itemId'
            });
            if (item) {
                return true;
            }
        };

        //搜索操作
        $scope.searchItems = function(itemId, $event) {
            $event.preventDefault();
            commonBizService.getItems({
                    searchKeywords: "item:" + itemId
                })
                .then(function(data) {
                    if (data && data.value) {
                        _.each($scope.items, function(item) {

                            if (item.fieldType == 'Date') {
                                item.propertyValue = '';
                            } else if (item.fieldName == 'tag') {
                                item.propertyValue = tabId;
                            } else {
                                item.propertyValue = data.value[0][item.fieldName];
                            }
                        });
                    } else {
                        alert("没有搜索到数据！！！");
                    }
                    console.log(data);
                }, function(data) {
                    console.log(data);
                })
        };

        //日历
        $scope.initTime = function() {
            $('.datetimepicker').datetimepicker({
                format: 'YYYY-MM-DD HH:mm'
            })
        };

        $scope.dateChange = function(item, index) {
            item.propertyValue = $('.datetimepicker' + index).val()
        };
    })
    .controller('ModalCopyDataCtrl', function($scope, $injector, $modalInstance, $log, targetListId, tabId, pageId, placeHolder, store) {
        var commonBizService = $injector.get("CommonBiz");
        $scope.profile = store.get("profile");
        $scope.cancel = function() {
            $modalInstance.dismiss('cancel');
        };

        /**
         * 复制模块数据
         * @param  {int} 原始模块数据id
         * @param  {int} 添加尾部
         * @return {array}
         */
        $scope.save = function(orgListId, addToEnd) {
            if(!orgListId){
                alert("请输入列表ID!");
                return;
            }
            commonBizService.cloneDataList(orgListId, addToEnd, targetListId)
                .then(function(data) {
                    console.log(data);
                    $route.reload();
                    // window.location.reload(true);
                }, function(data) {
                    alert(data);
                    console.log(data);
                });
        };

        /**
         * 替换当前模块数据
         * @param  {int} 原始模块数据id
         * @return {array}
         */
        $scope.replace = function(orgListId) {
            if(!orgListId){
                alert("请输入列表ID!");
                return;
            }
            if (window.confirm("覆盖操作不可逆，确定覆盖?")) {
                commonBizService.replaceDataList(orgListId, targetListId)
                    .then(function(data) {
                        console.log(data);
                        $route.reload();
                        // window.location.reload(true);
                    }, function(data) {
                        alert(data);
                        console.log(data);
                    });
            }
        };

        /**
         * 共享当前模块数据
         * @param  {int} 原始模块数据id
         * @return {array}
         */
        $scope.shareDataList = function(orgListId) {
            if(!orgListId){
                alert("请输入列表ID!");
                return ;
            }
            commonBizService.shareDataList(orgListId, targetListId, pageId, placeHolder)
                .then(function(data) {
                    console.log(data);
                    $route.reload();
                    // window.location.reload(true);
                }, function(data) {
                    alert(data);
                    console.log(data);
                });
        };
    }).controller('ModalBantchUpdateDataCtrl', function($scope, $injector, $modalInstance, $log, REJECT_BANTCHUPDATE_FIELDNAME, fields, cmsDatas, nations) {
        var commonBizService = $injector.get("CommonBiz");
        $scope.nations = nations;
        $scope.init = function() {
            $scope.fields = _.reject(fields, function(field) {
                return _.find(REJECT_BANTCHUPDATE_FIELDNAME, function(rfieldName) {
                    return rfieldName === field.fieldName;
                });
            });
            $scope.selectedField = $scope.fields ? $scope.fields[0] : null;
        };
        $scope.cancel = function() {
            $modalInstance.dismiss('cancel');
        };
        $scope.initTime = function() {
            $('.datetimepicker').datetimepicker({
                format: 'YYYY-MM-DD HH:mm'
            })
        };
        $scope.dateChange = function() {
            $scope.propertyValue = $('.datetimepicker').val()
        }
        $scope.save = function() {
            _.each(cmsDatas, function(cmsData) {
                var property = _.find(cmsData.properties, function(prop) {
                    return prop.propertyName == $scope.selectedField.fieldName;
                });
                if (property) {
                    //批量修改
                    property.propertyValue = $scope.propertyValue;
                } else {
                    //批量新增
                    property = {};
                    property.field = $scope.selectedField;
                    property.propertyName = $scope.selectedFieldName;
                    property.propertyValue = $scope.propertyValue;
                }
            });
            commonBizService.bantchUpdateCmsData(cmsDatas)
                .then(function() {
                    $route.reload();
                    // window.location.reload(true);
                });
        };
    });
